﻿using D.IdentityDbStore.DbContext;
using IdentityServer4.Stores;
using Microsoft.Extensions.Logging;
using SqlSugar;
using D.IdentityDbStore.Mappers;
using D.IdentityDbStore.Entities;

namespace D.IdentityDbStore.Stores
{
    /// <summary>
    /// 实现提取客户端存储信息
    /// </summary>
    public class ClientStore : IClientStore
    {
 		/// <summary>
        /// The DbContext.
        /// </summary>
        protected readonly ISqlSugarClient _db;

        /// <summary>
        /// The logger.
        /// </summary>
        protected readonly ILogger<ClientStore> _logger;
       
        public ClientStore(ILogger<ClientStore> logger, DbSql dbSql)
        {
            _logger = logger;
            _db = dbSql.Db;
        }
        /// <summary>
        /// 根据客户端ID 获取客户端信息内容
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public async Task<IdentityServer4.Models.Client> FindClientByIdAsync(string clientId)
        {
            var cModel = new IdentityServer4.Models.Client();

 
            //select * from Clients where ClientId=@client and Enabled=1
            var clients = await _db.Queryable<Client>()
                                           .Where(m => m.Enabled)
                                           .WhereIF(!string.IsNullOrEmpty(clientId), m => m.ClientId == clientId)
                                           .ToListAsync();

            /*select t2.* from Clients t1 
              inner join ClientGrantTypes t2 
              on t1.Id=t2.ClientId 
              where t1.ClientId=@client and Enabled=1
            */
            var clientGrantTypes = await _db.Queryable<ClientGrantType>().InnerJoin<Client>((a, b) => b.Id == a.ClientId)
                                             .Where((a, b) => b.Enabled && b.ClientId == clientId)
                                             .ToListAsync();

            /* select t2.* from Clients t1 
               inner join ClientRedirectUris t2 
               on t1.Id=t2.ClientId 
               where t1.ClientId=@client and Enabled=1 
            */
            var clientRedirectUris = await _db.Queryable<ClientRedirectUri>().InnerJoin<Client>((a, b) => b.Id == a.ClientId)
                                               .Where((a, b) => b.Enabled && b.ClientId == clientId)
                                               .ToListAsync();

            /*select t2.* from Clients t1 
              inner join ClientScopes t2 
              on t1.Id=t2.ClientId 
              where t1.ClientId=@client and Enabled=1
             */
            var clientScopes = await _db.Queryable<ClientScope>().InnerJoin<Client>((a, b) => b.Id == a.ClientId)
                                               .Where((a, b) => b.Enabled && b.ClientId == clientId)
                                               .ToListAsync();

            /* select t2.*from Clients t1 
               inner join ClientSecrets t2 
               on t1.Id = t2.ClientId 
               where t1.ClientId = @client and Enabled = 1
            */
            var clientSecrets = await _db.Queryable<ClientSecret>().InnerJoin<Client>((a, b) => b.Id == a.ClientId)
                                          .Where((a, b) => b.Enabled && b.ClientId == clientId)
                                          .ToListAsync();

            var _client = new Client();
            if (clients.Count > 0)
            {
                _client = clients[0];
                _client.AllowedGrantTypes = clientGrantTypes;
                _client.RedirectUris = clientRedirectUris;
                _client.AllowedScopes = clientScopes;
                _client.ClientSecrets = clientSecrets;

                cModel = _client.ToModel();
            }

            return cModel;
        }
    }
}
